//
//  GWTopicViewController.m
//  百思不得姐
//
//  Created by qianfeng on 16/3/3.
//  Copyright © 2016年 高炜. All rights reserved.
//

#import "GWTopicViewController.h"
#import <AFNetworking.h>
#import <UIImageView+WebCache.h>
#import <MJExtension.h>
#import "GWTopicModel.h"
#import <MJRefresh.h>
#import "GWTopicCell.h"
#import "GWComentViewController.h"
#import "GWNewViewController.h"
@interface GWTopicViewController ()
@property (nonatomic,copy)NSMutableArray *words;

@property (nonatomic,copy)NSString *maxtime;
@property (nonatomic,assign)NSInteger page;
@property (nonatomic,copy)NSDictionary *parames;

@property (nonatomic,assign)NSInteger lastSelectedIndex;
@end

static NSString *topicId =@"topic";
@implementation GWTopicViewController
-(NSMutableArray *)words
{
    if (_words==nil) {
        _words =[NSMutableArray array ];
    }
    return _words;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor =GWGlobalBGColor;
    self.tableView.separatorStyle =UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([GWTopicCell class]) bundle:nil ] forCellReuseIdentifier:topicId];
    [self setContentInset];
    
    
    [self setUpFooterAndHeaderView];
}
-(void)setContentInset
{
    self.tableView.contentInset =UIEdgeInsetsMake(GWEditorViewH+GWEditorViewY, 0, self.tabBarController.tabBar.height, 0);
    self.tableView.scrollIndicatorInsets =self.tableView.contentInset;
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(tabbarDidSelected) name:GWMyTabbarDidSelectedNotification object:nil];
}
-(void)tabbarDidSelected
{
    if (self.lastSelectedIndex==self.tabBarController.selectedIndex &&[self.tableView isShowingOnKeyWindow]) {
        [self.tableView.mj_header beginRefreshing];
    }
    self.lastSelectedIndex =self.tabBarController.selectedIndex;
}
-(void)setUpFooterAndHeaderView
{
    self.tableView.mj_header =[MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(loadNewTopic)];
    self.tableView.mj_header.automaticallyChangeAlpha =YES;
    [self.tableView.mj_header beginRefreshing];
    
    self.tableView.mj_footer =[MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMoreTopic)];
}
-(NSString *)a
{
    return [self.parentViewController isKindOfClass:[GWNewViewController class]]?@"newlist":@"list";
}
-(void)loadMoreTopic
{
    [self.tableView.mj_header endRefreshing];
    
    if(self.maxtime==nil)return;
    NSDictionary *parames =@{@"a":self.a,@"c":@"data",@"type":@(self.type),@"maxtime":self.maxtime,@"page":@(self.page+1)};
    self.parames =parames;
    [[AFHTTPSessionManager manager]GET:@"http://api.budejie.com/api/api_open.php" parameters:parames progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
       
        if (self.parames!=parames) return ;
        self.maxtime =responseObject[@"info"][@"maxtime"];
        NSArray *array =[GWTopicModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
        [self.words addObjectsFromArray:array] ;
        
        [self.tableView reloadData];
        
        [self.tableView.mj_footer endRefreshing];
        self.page ++;
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        [self.tableView.mj_footer endRefreshing];
    }];
}
-(void)loadNewTopic
{
    
    [self.tableView.mj_footer endRefreshing];
    NSDictionary *parames =@{@"a":self.a,@"c":@"data",@"type":@(self.type)};
    self.parames =parames;
    [[AFHTTPSessionManager manager]GET:@"http://api.budejie.com/api/api_open.php" parameters:parames progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
       
        if (self.parames!=parames) return ;
        
        self.maxtime =responseObject[@"info"][@"maxtime"];
        [self.words removeAllObjects];
        
        [self.words addObjectsFromArray:[GWTopicModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]]];
        
        
        [self.tableView reloadData];
        
        [self.tableView.mj_header endRefreshing];
        self.page=0;
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        
    }];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.words.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    GWTopicCell *cell = [tableView dequeueReusableCellWithIdentifier:topicId];
    
    GWTopicModel *word =self.words[indexPath.row];
    cell.topic =word;
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    GWTopicModel *word =self.words[indexPath.row];
    
    return word.cellHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    GWComentViewController *coment =[[GWComentViewController alloc]init];
    GWTopicModel *word =self.words[indexPath.row];
    coment.topic =word;
    [self.navigationController pushViewController:coment animated:YES];
}
@end
